package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/customer"
    customerReq "counter-help/admin/server/model/customer/request"
)

type CusReminderService struct {}

// CreateCusReminder 创建cusReminder表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusReminderService *CusReminderService) CreateCusReminder(cusReminder *customer.CusReminder) (err error) {
	err = global.GVA_DB.Create(cusReminder).Error
	return err
}

// DeleteCusReminder 删除cusReminder表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusReminderService *CusReminderService)DeleteCusReminder(ID string) (err error) {
	err = global.GVA_DB.Delete(&customer.CusReminder{},"id = ?",ID).Error
	return err
}

// DeleteCusReminderByIds 批量删除cusReminder表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusReminderService *CusReminderService)DeleteCusReminderByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]customer.CusReminder{},"id in ?",IDs).Error
	return err
}

// UpdateCusReminder 更新cusReminder表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusReminderService *CusReminderService)UpdateCusReminder(cusReminder customer.CusReminder) (err error) {
	err = global.GVA_DB.Model(&customer.CusReminder{}).Where("id = ?",cusReminder.ID).Updates(&cusReminder).Error
	return err
}

// GetCusReminder 根据ID获取cusReminder表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusReminderService *CusReminderService)GetCusReminder(ID string) (cusReminder customer.CusReminder, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&cusReminder).Error
	return
}

// GetCusReminderInfoList 分页获取cusReminder表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusReminderService *CusReminderService)GetCusReminderInfoList(info customerReq.CusReminderSearch) (list []customer.CusReminder, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&customer.CusReminder{})
    var cusReminders []customer.CusReminder
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	
	err = db.Find(&cusReminders).Error
	return  cusReminders, total, err
}